from re import I
from rest_framework.response import Response
from rest_framework.generics import GenericAPIView
from Apis.serializers import ProductModelSerializer
from product.models import Product
from rest_framework.permissions import IsAuthenticated
from rest_framework.authentication import BasicAuthentication
from rest_framework.pagination import LimitOffsetPagination

# 偏移量计算公式：offset = 每页记录数 x (当前页面 -1 )``
# 页码 = (偏移量 / 每页记录数) + 1

# 每页显示10条记录
# 第一页: offset 0 (从第一条开始)
# 偏移量 = 10 * (1-1) = 0
# 第二页：offset 10 (从第11条开始)
# 偏移量 = 10 * (2-1) = 10
# 第三页：offset 20 (从第21条开始)

# 自定义limit offset分页器
class CustomLimitOffsetPagination(LimitOffsetPagination):
    # 默认每页10条记录
    default_limit = 10 
    # 最大每页记录数
    max_limit = 20
    # 自定义查询参数名称
    limit_query_param = "limit"
    offset_query_param = "offset"

    def get_paginated_response(self, data):
        return Response({
            'code': 'success',
            'message': '请求成功！',
            'data': {
                'data':data,
                'count': self.count,
                'next': self.get_next_link(),
                'previous':self.get_previous_link(),
                'has_next': self.get_next_link() is not None,
                 'has_previous': self.get_previous_link() is not None,
            }
        })


# page=1&size=10
# limit=10&offset=20 从第21条记录开始，返回10条记录
class ProductGAPIView(GenericAPIView):
    # 定义属性
    # 接收的是查询之后的数据
    queryset = Product.objects.all()
    # 接收的是序列化器
    serializer_class = ProductModelSerializer
       # 数据查询完之后，再过滤
    # 分页
    pagination_class = CustomLimitOffsetPagination

    def get(self, request):
        # 获取全部数据
        product = self.get_queryset()
        pageinator = self.pagination_class()
        page = pageinator.paginate_queryset(product, request)
        
        # 判断
        if page is not None:
            ser = self.get_serializer(page, many=True)
            return pageinator.get_paginated_response(ser.data)
        ser = self.get_serializer(product, many=True)
        return Response({"message": "ok", "data": ser.data}, status=200)
        
    def post(self, request):
        # 接收前端的数据并反序列化
        ser = self.get_serializer(data = request.data)
        # 验证数据
        if ser.is_valid(raise_exception=True):
             # 保存数据 
            ser.save()
            return Response({"message": "ok", "data": ser.data}, status=201)
       
        
class ProductGdetailApiView(GenericAPIView):
    queryset = Product.objects.all()
    serializer_class = ProductModelSerializer

    def get(self, request, pk):
        product = self.get_object()
        ser = self.get_serializer(product)
        return Response({"message": "ok", "data": ser.data}, status=200)
    
    def put(self, request, pk):
        # 获取要修改的对象
        product = self.get_object()
        # 接收前端的数据并反序列化
        ser = self.get_serializer(product, data=request.data)
        # 验证数据
        if ser.is_valid(raise_exception=True):
            ser.save()
            return Response({"message": "ok", "data": ser.data}, status=200)

    def delete(self, request, pk):
        product = self.get_object()
        product.delete()
        return Response({"message": "ok"}, status=204)